﻿#ifndef __CLO_SDLCANVPLAYER_H__
#define __CLO_SDLCANVPLAYER_H__

#include "SDL_Include.h"
#include "SDL_Platform.h"

#define SDL_FREE_SURFACE(_pSurface) \
	if(_pSurface != NULL) {SDL_FreeSurface(_pSurface); (_pSurface)=NULL;}

/// SDL图像引擎画布类
class SDL_CCanvas : public GAF_CCanvas
{
public:
	SDL_CCanvas();
	~SDL_CCanvas();

protected:
	bool			OnInit(GAF_DWindowHandle hWindow);
	bool			OnRender();

public:
	void			AddGraphics(GAF_CGraphics* pGraphics);
	void			RemoveGraphics(GAF_CGraphics* pGraphics){};
	void			MoveGraphics(GAF_CGraphics* pGraphics, UINT nIndex){};
	void			CleanGarphicses();

private:
	bool			RenderImage(
					GAF_CBundle* pInformation,	// 信息
					GAF_DImageHandle hImage,		// 图像指针
					SDL_DPixelBit pb,		// 图像像素位数
					GAF_SRect rClip,			// 图像剪裁区
					int nPosX, int nPosY,		// 绘制坐标
					GAF_DAnchor aAnchor,		// 锚点
					GAF_SPoint atAnchorOffset,	// 锚点偏移
					BYTE bAlpha,			// 透明度
					double dAngle,			// 旋转
					double dZoomX,			// X轴缩放
					double dZoomY,			// Y轴缩放
					GAF_EThroughType through,	// 穿屏模式
					GAF_SRect rCanvasArea		// 目标绘制区域
					);

	bool			RenderText(
					GAF_CBundle* pInformation,	// 信息
					_STRING strText,		// 文字
					_STRING strFontPathName,	// 字体文件
					int nFontSize,			// 文字尺寸
					GAF_SLableStyle* pStyle,		// 文字样式（优先）
					GAF_SColour cFront,		// 文字颜色
					GAF_SColour cBack,		// 背景颜色
					int nPosX, int nPosY,		// 绘制坐标
					GAF_DAnchor aAnchor,		// 锚点
					GAF_SPoint atAnchorOffset,	// 锚点偏移
					BYTE bAlpha,			// 透明度
					double dAngle,			// 旋转
					double dZoom,			// 缩放
					GAF_EThroughType through,	// 穿屏模式
					GAF_SRect rCanvasArea		// 目标绘制区域
					);

private:
	// 渲染
	bool			RenderSurface(SDL_Surface* pSrcSurface,
					int nX, int nY, GAF_SRect rSrcClip, SDL_DPixelBit pb, BYTE bAlpha);
	// 以绘制方式
	bool			DrawSurface(SDL_Surface* pDisSurface, SDL_Surface* pSrcSurface,
					int nX, int nY, GAF_SRect rDisArea, GAF_SRect rSrcClip);
	// 以透明方式
	bool			AlphaSurface(SDL_Surface* pDisSurface, SDL_Surface* pSrcSurface,
					int nX, int nY, GAF_SRect rDisArea, GAF_SRect rSrcClip, BYTE bAlpha);
	// 以混合方式
	bool			BlendSurface(SDL_Surface* pDisSurface, SDL_Surface* pSrcSurface,
					int nX, int nY, GAF_SRect rDisArea, GAF_SRect rSrcClip, BYTE bAlpha);

	Uint32			AlphaPixel(Uint32 nDisColor, Uint32 nSrcColor, SDL_DPixelBit pbSrc, BYTE bAlpha);

	Uint32			GetPixel(SDL_Surface* pDisSurface, int nX, int nY);
	void			SetPixel(SDL_Surface* pDisSurface, int nX, int nY, Uint32 nColor);

private:
	void			ClearScreen();

	SDL_Surface*		SurfaceFormat(SDL_Surface* pSurface);

	SDL_Surface*		CreateText(TTF_Font* pTTF, _STRING strText, GAF_SColour cFront,
					GAF_SColour cBack = GAF_COLOUR_TRANSPARENT, int nFontStyle = 0);
	SDL_Surface*		CreateStyleText(TTF_Font* pTTF, _STRING strText, GAF_SLableStyle* pStyle,
					GAF_SColour cBack = GAF_COLOUR_TRANSPARENT, int nFontStyle = 0);			

private:
	GAF_SRect		m_rMainArea;
	SDL_Surface*		m_pMainSurface;
	GAF_CLink<GAF_CGraphics*>
				m_lnkGraphicses;

private:
	GAF_CTable<TTF_Font*>	m_tabFonts;

private:
	typedef _MAP<_STRING, SDL_Surface*>
				SDLBuffer;
	SDLBuffer		m_mapBuffer;
};

#endif